var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();
let interstitialAd = null;

function compareV(t, a) {
    if (t && a) {
        for (var e = t.split("."), n = a.split("."), o = Math.min(e.length, n.length), i = 0, s = 0; i < o && 0 == (s = parseInt(e[i]) - parseInt(n[i])); ) i++;
        return 0 <= (s = 0 != s ? s : e.length - n.length);
    }
    return console.log("版本号不能为空"), !1;
}

Page({
    data: {
        capitalized: [ "一", "二", "三", "四", "五", "六", "七" ],
        is_request: 1,
        sgin_succ: !1,
        videoAd: "",
        red_succ: !1,
        loading: !1
    },
    onShow: function() {
        var t = {
            action: "index",
            contr: "sign",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info), that.loadTask();
            var a = wx.getSystemInfoSync();
            t.info.config.adUnitId && compareV(a.SDKVersion, "2.6.0") && (that.data.videoAd = wx.createRewardedVideoAd({
                adUnitId: t.info.config.adUnitId
            }), that.data.videoAd.onError(function(t) {
                console.log(t);
            })), wx.createInterstitialAd && that.data.config.screen_ad && (that.data.interstitialAd = wx.createInterstitialAd({
                adUnitId: that.data.config.screen_ad
            }), that.data.interstitialAd.onError(function(t) {
                console.log(t);
            }), that.data.interstitialAd.show().catch(function(t) {
                console.error(t);
            }));
        });

        // 默认调用任务奖励
        // this.receive(2);

        // var t = wx.getStorageSync("task");
        // console.log(1111,t,Date.parse(new Date()),t.time,1e3 * parseInt(t.demo_time))
        // t && (Date.parse(new Date()) - t.time > 1e3 * parseInt(t.demo_time) ? that.receive(t.id) : wx.showModal({
        //     title: "温馨提示",
        //     content: "需要试玩" + t.demo_time + "秒才能获得奖励哦",
        //     showCancel: !1,
        //     success: function(t) {}
        // }), wx.removeStorageSync("task"));

        // 在页面onShow回调事件中创建插屏广告实例
        if (wx.createInterstitialAd) {
            interstitialAd = wx.createInterstitialAd({
            adUnitId: 'adunit-42e141bfcfdcc315'
            })
            interstitialAd.onLoad(() => {})
            interstitialAd.onError((err) => {})
            interstitialAd.onClose(() => {})
        };
        // 在适合的场景显示插屏广告
        if (interstitialAd) {
            interstitialAd.show().catch((err) => {
            console.error(err)
            })
        };


    },
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        });
    },
    loadTask: function() {
        var t = {
            action: "task",
            contr: "sign",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    onLoad: function(t) {
        that = this, t.parent_id && wx.setStorageSync("parent_id", t.parent_id);
    },
    sign: function() {
        this.data.config.sgin_subscribe ? wx.requestSubscribeMessage({
            tmplIds: [ that.data.config.sgin_subscribe ],
            success: function(t) {
                that.data.is_remind || that.remind();
            },
            fail: function(t) {},
            complete: function(t) {
                that.startSign();
            }
        }) : this.startSign();
    },
    startSign: function() {
        if (1 == this.data.is_request) {
            this.data.is_request = 2;
            var t = {
                action: "siginIn",
                contr: "sign",
                token: wx.getStorageSync("token")
            };
            _tools2.default.request(t, function(t) {
                that.data.is_request = 1, that.setData({
                    sgin_succ: !0
                }), that.setData(t.info), that.onReady();
            });
        }
    },
    complete: function(t) {
        var a = t.currentTarget.dataset.index;
        console.log('complete'), console.log(that.data.task[a].type), 1 == that.data.task[a].type ? wx.navigateTo({
            url: "/bh_bargain/pages/luck/luck"
        }) : 2 == that.data.task[a].type ? that.videoAdShow(t) : 3 == that.data.task[a].type && wx.navigateToMiniProgram({
            appId: that.data.task[a].appid,
            path: that.data.task[a].path,
            extraData: {
                foo: "bar"
            },
            envVersion: "develop",
            success: function(t) {
                that.bindsuccess(a);
            }
        });
    },
    bindsuccess: function(t) {
        var a = {
            id: that.data.task[t].id,
            demo_time: that.data.task[t].number,
            time: Date.parse(new Date())
        };
        wx.setStorageSync("task", a);
    },
    cli_receive: function(t) {
        var a = t.currentTarget.dataset.index, e = that.data.task[a].id;
        this.receive(e);
    },
    receive: function(t) {
        var a = {
            action: "receive",
            contr: "sign",
            id: t,
            token: wx.getStorageSync("token")
        };
        1 == this.data.is_request && (this.data.is_request = 2, _tools2.default.request(a, function(t) {
            that.loadTask(), that.data.is_request = 1
            // wx.showToast({
            //     title: "领取奖励成功"
            // });
        }));
    },
    switch1Change: function() {
        this.data.config.sgin_subscribe && (that.data.is_remind ? that.remind() : wx.requestSubscribeMessage({
            tmplIds: [ that.data.config.sgin_subscribe ],
            success: function(t) {
                that.remind();
            }
        }));
    },
    videoAdShow: function(e) {
        if (this.data.videoAd) {
            var n = e.currentTarget.dataset.type;
            that.data.is_video = 2, this.data.videoAd.show().catch(function() {
                that.data.videoAd.load().then(function() {
                    return that.data.videoAd.show();
                }).catch(function(t) {
                    that.data.is_video = 1, wx.showToast({
                        icon: "none",
                        title: "激励视频加载失败~"
                    }), console.log("激励视频 广告显示失败");
                });
            }), that.data.videoAd.offError(function(t) {
                that.data.is_video = 1, console.log(t);
            }), that.data.videoAd.onClose(function(t) {
                if (that.data.is_video = 1, that.data.videoAd.offClose(), t && t.isEnded || void 0 === t) if (1 == n) that.double(); else if (2 == n) {
                    var a = e.currentTarget.dataset.index;
                    that.receive(that.data.task[a].id);
                }
            });
        }
    },
    double: function() {
        if (1 == this.data.is_request) {
            this.data.is_request = 2;
            var t = {
                action: "double",
                contr: "sign",
                token: wx.getStorageSync("token")
            };
            _tools2.default.request(t, function(t) {
                that.data.is_request = 1, wx.showToast({
                    title: "领取成功"
                }), that.setData({
                    sgin_succ: !1
                }), that.onReady();
            });
        }
    },
    receiveBag: function(t) {
        var a = t.currentTarget.dataset.id;
        if (1 == this.data.is_request) {
            this.data.is_request = 2;
            var e = {
                action: "receive",
                contr: "red",
                token: wx.getStorageSync("token"),
                id: a
            };
            _tools2.default.request(e, function(t) {
                that.data.is_request = 1, 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : (that.setData(t.info), that.redSuccStatus(), that.onReady());
            });
        }
    },
    redSuccStatus: function() {
        this.setData({
            red_succ: !this.data.red_succ
        });
    },
    remind: function() {
        var t = {
            action: "remind",
            contr: "sign",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            var a = !that.data.is_remind;
            that.setData({
                is_remind: a
            });
        });
    },
    sginSuccStatus: function() {
        that.setData({
            sgin_succ: !this.data.sgin_succ
        });
    },
    getPhoneNumber: function(t) {
        if (console.log(t), "getPhoneNumber:ok" == t.detail.errMsg) {
            var a = {
                action: "bindPhone",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: t.detail.encryptedData,
                iv: t.detail.iv
            };
            _tools2.default.request(a, function(t) {
                wx.showToast({
                    title: "绑定成功"
                }), that.onReady();
            });
        }
    },
    getUserInfo: function(t) {
        if ("getUserInfo:ok" == t.detail.errMsg) {
            var a = {
                action: "register",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: t.detail.encryptedData,
                iv: t.detail.iv
            };
            _tools2.default.request(a, function(t) {
                wx.showToast({
                    title: "授权成功"
                }), that.onShow();
            });
        }
    },
    gotolog: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/integralLog/integralLog"
        });
    },
    gotoShop: function() {
        wx.switchTab({
            url: "/bh_bargain/pages/goodsconvert/goodsconvert"
        });
    },
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_bargain/pages/group/group?parent_id=" + this.data.share.member_id
        };
    },
    gotoGoods: function(t) {
        var a = t.currentTarget.dataset.index;
        1 == this.data.integral_goods[a].type ? wx.navigateTo({
            url: "/bh_bargain/pages/integral_goods/integral_goods?id=" + this.data.integral_goods[a].id
        }) : wx.navigateTo({
            url: "/bh_bargain/pages/pdd_details/pdd_details?id=" + this.data.integral_goods[a].id + "&pdd_id=" + this.data.integral_goods[a].relation_goods_id
        });
    }
});